<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/27
 * Time: 10:39
 */

namespace Biz\validate;

use Biz\model\ShopCategoryEntrance as mShopCategoryEntrance;

class CategoryEntrance extends \Doraemon\pockets\validate\Base
{
    public static function rules()
    {
        return [

            ['banner', 'required', null, '请选择banner', ['create', 'update']],

            ['title',  'required', null, '标题不能为空',  ['create', 'update']],
            ['title',  'length', '1,4', '分类名称最大4个字符', ['create', 'update']],

            ['start_time', 'required', null, '请选择开始日期', ['create', 'update']],

            ['link', 'required', null, '请输入链接', ['create', 'update']],

            ['sort', 'required', null, '请填写权重值', ['create', 'update']],
            ['sort', 'regex',['pattern'=>'/^[\0-9]+$/u'],'权重只能输入数字',['create','update']],
            ['sort', 'callback', ['callback' => [new self(), 'checkWeight']], '当前权重值已存在', ['create', 'update']],

        ];
    }

    public function checkWeight($value, $valueIdentifier, $allData)
    {
        $categoryEntranceId = '';
        if (!empty($allData['categoryentranceid'])){
            $categoryEntranceId = $allData['categoryentranceid'];
        }

        $weight = mShopCategoryEntrance::checkWeight($allData['sort'], $categoryEntranceId);
        if ($weight){

            return false;
        }

        return true;

    }
}